
package com.hlkj.pay.task;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.pay.app.order.IAcctOrderAppService;
import com.hlkj.pay.app.order.IRefundOrderAppService;
import com.hlkj.pay.dto.order.PayOrderFundQueryDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.infrastructure.model.order.OrderFundDO;
import com.hlkj.pay.service.order.IPayOrderFundService;
import com.hlkj.pay.util.DateUtils;
import com.hlkj.pay.util.OrderCodeUtil;
import com.hlkj.pay.vo.openapi.order.resp.AcctPayOrderApiResp;
import com.hlkj.pay.vo.order.req.AcctPayOrderParamReq;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/29 10:43
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class AcctOrderTasks {

    private final IAcctOrderAppService acctOrderAppService;

    private final IRefundOrderAppService refundOrderAppService;

    private final IPayOrderFundService payOrderFundService;

    private final OrderCodeUtil orderCodeUtil;

    @Value("${server.port:8020}")
    private String serverPort;

    @Value("${hlkj.task.port:8020}")
    private String taskPort;

    /**
     * 每天凌晨分账重新提交
     */
    @Scheduled(cron = "0 0 0 * * *")
    public void accRetry() {
        if (!serverPort.equals(taskPort)) {
            log.info("当前accRetry task port 非目标port serverPort:{},taskPort:{}", serverPort, taskPort);
            return;
        }
        log.info("分账重新提交开始");
        PayOrderFundQueryDto payOrderFundQueryDto = new PayOrderFundQueryDto();
        payOrderFundQueryDto.setSystemProcessType(PayOrderEnums.SYSTEM_PROCESS_TYPE.SYSTEM.getCode());
        payOrderFundQueryDto.setFundStatus(PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL.getCode());
        payOrderFundQueryDto.setFundType(CommonEnum.YES_NO_TYPE.NO.getCode());
        // 昨天
        Date yesterday = DateUtil.offsetDay(new Date(),-1);
        payOrderFundQueryDto.setStartQueryTime(DateUtils.shanghaiStartDate(DateUtil.format(yesterday, DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN));
        payOrderFundQueryDto.setEndQueryTime(DateUtils.shanghaiEndDate(DateUtil.format(yesterday,DatePattern.NORM_DATE_PATTERN), DatePattern.NORM_DATE_PATTERN));
        log.info("分账重新提交开始 请求参数：{}", JSONUtil.toJsonStr(payOrderFundQueryDto));
        List<OrderFundDO> payOrderFundList = payOrderFundService.queryPayAcctInfoList(payOrderFundQueryDto);
        log.info("分账重新提交开始 响应参数:{}",payOrderFundList);

        AcctPayOrderParamReq acctPayOrderParamReq = null;
        if(CollUtil.isNotEmpty(payOrderFundList)){
            for(OrderFundDO orderFundDO:payOrderFundList){
                acctPayOrderParamReq = new AcctPayOrderParamReq();
                acctPayOrderParamReq.setPayOrderNo(orderFundDO.getPayOrderNo());
                acctPayOrderParamReq.setMchAcctOrderNo(orderCodeUtil.orderNo("M",1));
                acctPayOrderParamReq.setAcctInfos(payOrderFundService.queryAcctAccountDetailInfo(orderFundDO.getBatchNo(),PayOrderEnums.PAY_ORDER_FUND_STATUS.FAIL.getCode(),null));
                acctPayOrderParamReq.setAccRetry(1);
                acctPayOrderParamReq.setOldBatchNo(orderFundDO.getBatchNo());
                CommonResult<AcctPayOrderApiResp> payOrderRespCommonResult = acctOrderAppService.submitAcctOrder(acctPayOrderParamReq);
                log.info("accRetry task :{}", JSONUtil.toJsonStr(payOrderRespCommonResult));
            }
        }
    }

    // 2点进行统计
    @Scheduled(cron = "0 0/10 * * * *")
    public void cronTask() {
        if (!serverPort.equals(taskPort)) {
            log.info("当前task port 非目标port serverPort:{},taskPort:{}", serverPort, taskPort);
            return;
        }
        acctOrderAppService.syncPayAcctOrderStatus();
        acctOrderAppService.syncRefundAcctOrderStatus();
        refundOrderAppService.syncRefundOrderStatus();
    }

}
